package com.cslg.util;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/CheckCodeUtil")
public class CheckCodeUtil extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //定义图形验证码的长和宽
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100,4,50);
        String code = lineCaptcha.getCode();
        System.out.println("产生的验证码是："+code);
        //将产生的验证码存放到session中
        /*
            1. page域：作用于1当前页面，可以指定存放的域
            2. request域：作用于当次请求
            3. session域：作用于当次会话，默认有效时间是30分钟，30分钟内浏览器没有关闭都可以使用
            4. application域：慎用，是最大的域，作用于整个项目
        * */
        HttpSession session = request.getSession();
        session.setAttribute("code", code);//将验证码保存到session中
        //将验证码图片输出到浏览器
        lineCaptcha.write(response.getOutputStream());
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
